<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 打印流详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .table-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .method-badge {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        }
        .constructor-badge {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-4">
                <i class="fas fa-print mr-4"></i>Java 打印流
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">PrintStream 与 PrintWriter 的深度解析</p>
            <div class="flex justify-center space-x-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-code mr-2"></i>字节流
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-font mr-2"></i>字符流
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-database mr-2"></i>任意数据类型
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <h2 class="text-3xl font-bold mb-6 text-gradient">
                <i class="fas fa-info-circle mr-3"></i>什么是打印流？
            </h2>
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">打</span>印流是 Java I/O 体系中的重要组成部分，主要包含了字节打印流（PrintStream）和字符打印流（PrintWriter）。它们最大的特点是可以方便地打印任何数据类型，为开发者提供了统一且便捷的输出接口。
            </p>
        </div>

        <!-- Visual Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-gradient">
                <i class="fas fa-sitemap mr-3"></i>打印流体系结构
            </h2>
            <div class="mermaid">
                graph TD
                    A[打印流] --> B[PrintStream<br/>字节打印流]
                    A --> C[PrintWriter<br/>字符打印流]
                    B --> D[继承自 OutputStream]
                    C --> E[继承自 Writer]
                    B --> F[System.out]
                    B --> G[System.err]
                    C --> H[文件输出]
                    C --> I[网络输出]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Features Grid -->
        <div class="grid md:grid-cols-3 gap-6 mb-12">
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                <div class="text-4xl text-purple-600 mb-4">
                    <i class="fas fa-magic"></i>
                </div>
                <h3 class="text-xl font-bold mb-2">自动刷新</h3>
                <p class="text-gray-600">支持自动刷新功能，确保数据及时输出到目标设备</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                <div class="text-4xl text-purple-600 mb-4">
                    <i class="fas fa-shield-alt"></i>
                </div>
                <h3 class="text-xl font-bold mb-2">异常处理</h3>
                <p class="text-gray-600">不会抛出 IOException，简化了错误处理流程</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                <div class="text-4xl text-purple-600 mb-4">
                    <i class="fas fa-puzzle-piece"></i>
                </div>
                <h3 class="text-xl font-bold mb-2">格式化输出</h3>
                <p class="text-gray-600">支持 printf 风格的格式化输出，灵活控制输出格式</p>
            </div>
        </div>

        <!-- PrintStream Methods Table -->
        <div class="bg-white rounded-2xl shadow-xl overflow-hidden mb-12">
            <div class="table-header text-white p-6">
                <h2 class="text-3xl font-bold">
                    <i class="fas fa-list-alt mr-3"></i>PrintStream 类常用方法
                </h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="bg-gray-50 border-b">
                            <th class="px-6 py-4 text-left text-sm font-bold text-gray-700">序号</th>
                            <th class="px-6 py-4 text-left text-sm font-bold text-gray-700">方法签名</th>
                            <th class="px-6 py-4 text-left text-sm font-bold text-gray-700">类型</th>
                            <th class="px-6 py-4 text-left text-sm font-bold text-gray-700">描述</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="border-b hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4">
                                <span class="text-purple-600 font-bold">01</span>
                            </td>
                            <td class="px-6 py-4">
                                <code class="bg-gray-100 px-2 py-1 rounded text-sm">public PrintStream(File file) throws FileNotFoundException</code>
                            </td>
                            <td class="px-6 py-4">
                                <span class="constructor-badge text-white px-3 py-1 rounded-full text-sm">构造</span>
                            </td>
                            <td class="px-6 py-4 text-gray-600">通过一个 File 对象实例化 PrintStream 类</td>
                        </tr>
                        <tr class="border-b hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4">
                                <span class="text-purple-600 font-bold">02</span>
                            </td>
                            <td class="px-6 py-4">
                                <code class="bg-gray-100 px-2 py-1 rounded text-sm">public PrintStream(OutputStream out)</code>
                            </td>
                            <td class="px-6 py-4">
                                <span class="constructor-badge text-white px-3 py-1 rounded-full text-sm">构造</span>
                            </td>
                            <td class="px-6 py-4 text-gray-600">接受 OutputStream 对象，实例化 PrintStream 类</td>
                        </tr>
                        <tr class="border-b hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4">
                                <span class="text-purple-600 font-bold">03</span>
                            </td>
                            <td class="px-6 py-4">
                                <code class="bg-gray-100 px-2 py-1 rounded text-sm">public void print(boolean b)</code>
                            </td>
                            <td class="px-6 py-4">
                                <span class="method-badge text-white px-3 py-1 rounded-full text-sm">普通</span>
                            </td>
                            <td class="px-6 py-4 text-gray-600">此方法被多次重载，输出任意类型</td>
                        </tr>
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4">
                                <span class="text-purple-600 font-bold">04</span>
                            </td>
                            <td class="px-6 py-4">
                                <code class="bg-gray-100 px-2 py-1 rounded text-sm">public void println(boolean b)</code>
                            </td>
                            <td class="px-6 py-4">
                                <span class="method-badge text-white px-3 py-1 rounded-full text-sm">普通</span>
                            </td>
                            <td class="px-6 py-4 text-gray-600">此方法被重载多次，输出任意数据类型后换行</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Code Example -->
        <div class="bg-gray-900 rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-white">
                <i class="fas fa-code mr-3"></i>代码示例
            </h2>
            <pre class="text-green-400 overflow-x-auto"><code>// 使用 PrintStream 输出到文件
PrintStream ps = new PrintStream(new File("output.txt"));
ps.println("Hello, PrintStream!");
ps.printf("格式化输出: %d + %d = %d%n", 10, 20, 30);
ps.close();

// 使用 System.out (也是 PrintStream)
System.out.println("控制台输出");
System.out.printf("当前时间: %tF %tT%n", new Date(), new Date());</code></pre>
        </div>

        <!-- Best Practices -->
        <div class="bg-white rounded-2xl shadow-xl p-8">
            <h2 class="text-3xl font-bold mb-6 text-gradient">
                <i class="fas fa-lightbulb mr-3"></i>最佳实践
            </h2>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 text-xl mr-3 mt-1"></i>
                    <div>
                        <h3 class="font-bold mb-2">使用 try-with-resources</h3>
                        <p class="text-gray-600">确保资源正确关闭，避免资源泄漏</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 text-xl mr-3 mt-1"></i>
                    <div>
                        <h3 class="font-bold mb-2">选择合适的打印流</h3>
                        <p class="text-gray-600">处理文本优先使用 PrintWriter，二进制数据使用 PrintStream</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 text-xl mr-3 mt-1"></i>
                    <div>
                        <h3 class="font-bold mb-2">注意字符编码</h3>
                        <p class="text-gray-600">在创建打印流时指定正确的字符编码，避免乱码问题</p>
                    </div>
                </div>
                <div class="flex items-start">
                    <i class="fas fa-check-circle text-green-500 text-xl mr-3 mt-1"></i>
                    <div>
                        